home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / reve / reve_proc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.8 KB  |  207 lines

  1.  
  2. /*  @(#)reve_proc.c 1.9 91/11/07
  3.  *
  4.  *  Main routine for the separate play_reve program.
  5.  *
  6.  *  Copyright (C) 1990, 1991 - Rich Burridge & Yves Gallot.
  7.  *  All rights reserved.
  8.  *
  9.  *  Permission is granted to copy this source, for redistribution
  10.  *  in source form only, provided the news headers in "substantially
  11.  *  unaltered format" are retained, the introductory messages are not
  12.  *  removed, and no monies are exchanged.
  13.  *
  14.  *  Permission is also granted to copy this source, without the
  15.  *  news headers, for the purposes of making an executable copy by
  16.  *  means of compilation, provided that such copy will not be used
  17.  *  for the purposes of competition in any othello tournaments, without
  18.  *  prior permission from the authors.
  19.  *
  20.  *  No responsibility is taken for any errors on inaccuracies inherent
  21.  *  either to the comments or the code of this program, but if reported
  22.  *  (see README file), then an attempt will be made to fix them.
  23.  */
  24.  
  25. #include "reve.h"
  26.  
  27. struct reve_in in ;          /* Input supplied on standard input. */
  28. struct reve_out out ;        /* Results written to standard output. */
  29.  
  30. int sin ;                    /* Size of the input buffer. */
  31.  
  32. int debug   = FALSE ;        /* If set, prints out various debug messages. */
  33. int saveres = FALSE ;        /* If set, save computer results to log file. */
  34.  
  35. char edgefile[MAXLINE] ;     /* Location of the reve edge table file. */
  36. char progname[MAXLINE] ;     /* The name of this program. */
  37.  
  38. /* REVE global variables */
  39.  
  40. int damier[NIVEAUMAX][64] ;  /* Boards at different depth level */
  41. int tacouleur, macouleur ;   /* Your and my colors during evaluation */
  42. int mnb, profmax ;           /* Number of moves played, current max. depth */
  43. int max_depth = 2 ;          /* Computer strategy - maximum depth. */
  44. int vp0, vo0 ;               /* Current mobility components */
  45. long c1, c2, c3 ;            /* Constants used in evaluation function */
  46. long edges[6561] ;           /* Edges Stability Table */
  47. long note ;                  /* Note value for current computer move. */
  48. time_t savetime ;            /* Restore correct time after a suggestion. */
  49. time_t timeleft ;            /* Amount of time left for computer moves. */
  50.  
  51. FILE *find_file      P((char *)) ;
  52.  
  53. int main             P((int, char **)) ;
  54.  
  55. void get_options     P((int, char **)) ;
  56. void getparam        P((char *, char **, char *)) ;
  57. void init_edge_table P((char *)) ;
  58. void show_best       P((int, long)) ;
  59.  
  60.  
  61. /*ARGSUSED*/
  62. int
  63. main(argc, argv)
  64. int argc ;
  65. char *argv[] ;
  66. {
  67.   int reply ;
  68.  
  69.   STRCPY(progname, argv[0]) ;   /* Save program name for later use. */
  70.   get_options(argc, argv) ;     /* Extract command line options. */
  71.   for (;;)
  72.     {
  73.       sin = sizeof(struct reve_in) ;
  74.       if ((reply = read(0, (char *) &in, sin)) > 0)
  75.         {
  76.                if (in.type == M_TIME)    timeleft  = in.timeleft ;
  77.           else if (in.type == M_PROFMAX) max_depth = in.level ;
  78.           else
  79.             {
  80.               if (in.type == M_SUGGESTION) savetime = timeleft ;
  81.               play_reve(in.board, in.player, in.level,
  82.                         &out.move, (long *) &out.note) ;
  83.               out.type = in.type ;
  84.               WRITE(1, (char *) &out, sizeof(struct reve_out)) ;
  85.               if (in.type == M_SUGGESTION) timeleft = savetime ; 
  86.             }
  87.         }
  88.       else if (reply == 0) exit(1) ;
  89.     }
  90. }
  91.  
  92.  
  93. FILE *
  94. find_file(filename)
  95. char *filename ;
  96. {
  97.   char name[MAXLINE], *paths, *ptr ;
  98.   int i = 0 ;
  99.   FILE *fp = NULL ;
  100.  
  101.   if ((fp = fopen(filename, "r")) == NULL)
  102.     {
  103.       paths = getenv("PATH") ;
  104.       if ((ptr = paths) && filename[0] != '/')
  105.         for (;;)
  106.           if (*ptr == ':' || *ptr == 0)
  107.             {
  108.               if (*ptr == 0) break ;
  109.               name[i++] = '/' ;
  110.               name[i] = 0 ;
  111.               STRCAT(name, edgefile) ;
  112.               if ((fp = fopen(name, "r")) != NULL) break ;
  113.               if (*ptr == '\0') break ;
  114.               ptr++ ;
  115.               i = 0 ;
  116.             }
  117.           else name[i++] = *ptr++ ;
  118.     }
  119.   return(fp) ;
  120. }
  121.  
  122.  
  123. void
  124. get_options(argc, argv)
  125. int argc ;
  126. char *argv[] ;
  127. {
  128.   INC ;
  129.   while (argc > 0)
  130.     {
  131.       if (argv[0][0] == '-')
  132.         switch (argv[0][1])
  133.           {
  134.             case 'd' : if (EQUAL(argv[0], "-debug")) debug = TRUE ;
  135.                        break ;
  136.             case 'e' : INC ;
  137.                        getparam(edgefile, argv, "-e needs an edgetable file") ;
  138.                        init_edge_table(edgefile) ;
  139.                        break ;
  140.             case 'l' : if (EQUAL(argv[0], "-log")) saveres = TRUE ;
  141.           }
  142.       INC ;
  143.     }
  144. }
  145.  
  146.  
  147. void
  148. getparam(s, argv, errmes)
  149. char *s, *argv[], *errmes ;
  150. {
  151.   if (*argv != NULL && argv[0][0] != '-') STRCPY(s, *argv) ;
  152.   else
  153.     { 
  154.       FPRINTF(stderr,"%s: %s as next argument.\n", progname, errmes) ;
  155.       exit(1) ;
  156.     }
  157. }
  158.  
  159.  
  160. void
  161. init_edge_table(edgefile)     /* Load reve edge table values. */
  162. char *edgefile ;
  163. {
  164.   char buf[MAXLINE], *ptr ;
  165.   int i, line ;
  166.   FILE *fp = NULL ;
  167.  
  168.   if ((fp = find_file(edgefile)) == NULL)
  169.     {
  170.       FPRINTF(stderr, "Cannot open Edge Stability Table file\n") ;
  171.       exit(1) ;
  172.     }
  173.   line = 0 ;
  174.   while (fgets(buf, MAXLINE, fp) != NULL)
  175.     {
  176.       line++ ;
  177.       if (buf[0] == '\n' || buf[0] == '#') continue ;
  178.       if ((ptr = index(buf, '[')) == NULL)
  179.         {
  180.           FPRINTF(stderr, "Cannot read edge table file at line %d\n", line) ;
  181.           exit(1) ;
  182.         }
  183.       SSCANF(ptr+1, "%d", &i) ;
  184.       if ((ptr = index(buf, '=')) == NULL)
  185.         {
  186.           FPRINTF(stderr, "Cannot read edge table file at line %d\n", line) ;
  187.           exit(1) ;
  188.         }
  189.       SSCANF(ptr+1, "%ld", &edges[i]) ;
  190.     }
  191.   FCLOSE(fp) ;
  192.   for (i = 0 ; i < 3281; i++) edges[6560 - i] = - edges[i] ;
  193. }
  194.  
  195.  
  196. void
  197. show_best(move, note)
  198. int move ;
  199. long note ;
  200. {
  201.   out.type  = M_BEST ;
  202.   out.move  = move ;
  203.   out.note  = note ;
  204.   out.depth = profmax ;
  205.   WRITE(1, (char *) &out, sizeof(struct reve_out)) ;
  206. }
  207.